<?php

namespace app\helps;
use app\facade\Redis;
use think\Db;

class BookRedis{

    /**
     * 订单预定
     * @param $user
     * @param $goodid
     * @return string
     */
    public function setUserBook($user,$good) {
        $uid =$user["id"];
        $data=[];
        $data["uid"]=$uid;
        $data["username"]=$user["username"];
        //修改成紧急联系人
        $data["nick_name"]=$user["contact"];
        $data["status"]=1;
        $data["gid"]=$good["id"];
        $data["gname"]=$good["name"];
        $data["weight"]=$this->getBookWeight($user["weight"]);
        $data["create_time"]=date('Y-m-d H:i:s');
        $book_id=Db::table("tb_mt_book")->insertGetId($data);
        if (!$book_id){
            return false;
        }
        Redis::hset("book_".date("Y-m-d"),$uid.":".$good["id"],$book_id);
        return true;

    }

    /**
     * 产品是否被预定过
     * @param $uid
     * @param $goodid
     * @return bool
     */
    public function userBook($uid,$goodid) {
        $book_id=Redis::hget("book_".date("Y-m-d"),$uid.":".$goodid);
        if ($book_id){
           return true;
        }
        return false;
    }

    /**
     * 预定中奖
     * @param $uid
     * @param $goodid
     * @return bool
     */
    public function setUserBookResul($uid,$goodid) {
        $book_id=Redis::hget("book_".date("Y-m-d"),$uid.":".$goodid);
        if ($book_id){
            $updateData= [];
            $updateData["status"]=2;
            Db::table("tb_mt_book")->where("id",$book_id)->update($updateData);
        }
        return true;
    }

    /**
     * 设置所有预定中的产品为 未中奖
     * @param $goodid
     * @return bool
     */
    public function cleanBoolByGid($goodid) {
        $updateData= [];
        $updateData["status"]=3;
        Db::table("tb_mt_book")->where("gid",$goodid)->where("status",1)->update($updateData);
        return false;
    }

    /**
     * 设置所有预定中的产品为 未中奖
     * @param $goodid
     * @return bool
     */
    public function cleanBoolByGidUid($goodid,$uid) {
        $updateData= [];
        $updateData["status"]=4;
        Db::table("tb_mt_book")->where("gid",$goodid)->where("uid",$uid)->where("status",1)->update($updateData);
        return false;
    }

    /**
     * 清空redis 的预定
     * @param $date
     */
    public function userBookCleanByDate($date) {
        Redis::del("book_".$date);
    }

    /**
     * 清空redis 的预定
     * @param $date
     */
    public function userBookCleanByDateAndUid($date,$uid,$gid) {
        Redis::hset("book_".$date,$uid.":".$gid,0);
    }


    /**
     * 获取预定订单权重（更具用户权重随机）
     * 用户权重 为0 则设置 订单权重为0 ；
     * 如果用户权重100 ，则设置订单权重100；
     * 其他 情况则 给一个1 到 100 的随机中奖率，
     * 这个中奖率乘以 用户权重 就是 这个订单的权重
     * @param $date
     */
    private function getBookWeight($userWeight) {
        if ($userWeight==0||$userWeight==100){
            return $userWeight;
        }
        //随机1-100 一个数
        $rand=rand(1,100);
        return $rand*$userWeight/100;
    }


    public function userLotteryResult($uid,$gid){
        $book_id=Redis::hget("book_".date("Y-m-d"),$uid.":".$gid);
        if ($book_id){
           return Db::table("tb_mt_book")->where("id",$book_id)->value("status");
        }
        return -1;
    }

}